<?php
namespace app\admin\controller;
use app\admin\model\GoodsModel;
use think\Controller;
use \think\Request;
use app\admin\controller\Base;
class Goods extends Base{
    // 商城列表显示
    public function index(){
        $this->isLogin();
        $goods_name = input('goods_name');
        $rows = new GoodsModel();
        $second = new GoodsModel(); // 三级联动
        $rows = $rows->index($goods_name);
        $page = $rows->render();
        $second = $second->getrows();
        $this->assign('goods_name',$goods_name);
        $this->assign('second',$second);
        $this->assign('rows',$rows);
        $this->assign('page',$page);
        return $this->fetch();
    }

    // 三级联动
    public function getChild(){
        $this->isLogin();
        $id = input('id');
        $rows = new GoodsModel();
        $rows = $rows->getSecond($id);
        echo json_encode($rows);
    }

    // 删除
    public function remove(){
        $this->isLogin();
        $id = input('id');
        $data = [
            'id'=>$id,
            'is_del'=> 1
        ];
        $res = new GoodsModel();
        $res = $res->getRemove($data);
        return $res;
    }

    // 修改 (显示)

    public function update(){
        $this->isLogin();
        $id = input('id');
        $res = new GoodsModel();
        $second = new GoodsModel();
        $second = $second->getrows();
        $res = $res->getUpdate($id);
        $this->assign('second',$second);
        $this->assign('res',$res);
        return $this->fetch('update_goods');
    }

    /*
     * 修改
     * */
    public function toUpdate(){
        $this->isLogin();
        $id = input('id');
        $data = [
            'id'=>$id,
            'goods_url'=>input('goods_url'),
            'goods_name'=>input('goods_name'),
            'goods_price'=>input('goods_price'),
            'goods_cat_id1'=>input('goodsCatId1'),
            'goods_cat_id2'=>input('goodsCatId2'),
            'goods_cat_id3'=>input('goodsCatId3'),
            'create_time'=>time()
        ];
        $res = new GoodsModel();
        $res = $res->getRemove($data);
        return $res;
    }
        public function getImg(){
            $this->isLogin();
            if(Request::instance()->isAjax()){
                $file = request()->file('goods_url');
                if($file){
                    $info = $file->move(ROOT_PATH . 'public' . DS . 'upload' .DS . 'shoppimg' );
                    if($info){
                        echo '/upload/'.'shoppimg'.'/'.date('Ymd').'/'.$info->getFilename();
                    }else{
                        echo $file->getError();
                    }
                }
            }
        }



    // 增加商品(显示)
        public function insert(){
            $this->isLogin();
            $second = new GoodsModel();
            $second = $second->getrows();
            $this->assign('second',$second);
            return $this->fetch('add_goods');
        }


    // 增加商品
    public function addInsert(){
        $this->isLogin();
        $data = [
            'goods_url'=>input('goods_url'),
            'goods_name'=>input('goods_name'),
            'goods_price'=>input('goods_price'),
            'goods_stock'=>input('goods_stock'),
            'initial_stock'=>input('goods_stock'),
            'goods_cat_id1'=>input('goodsCatId1'),
            'goods_cat_id2'=>input('goodsCatId2'),
            'goods_cat_id3'=>input('goodsCatId3'),
            'create_time'=>time()
        ];
        $row = new GoodsModel();
        $row = $row->getInsert($data);
        return $row;
    }

    // 批量删除数据
    public function delSelect(){
        $this->isLogin();
        $id = input('ids');
        $str = $id;
        $goods_id = explode(',',$str);
        foreach($goods_id as $id){
            $res = new GoodsModel();
            $res = $res->delAll($id);
        }
        return $res;
    }

    // 精品设置
    public function boutique(){
        $this->isLogin();
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new GoodsModel();
            $res = $res->getbest($id);
        }
        return $res;
    }
    // 取消精品
    public function cancelBoutique(){
        $this->isLogin();
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new GoodsModel();
            $res = $res->getCancelbest($id);
        }
        return $res;
    }

//    设置今日必抢

    public function batchActive(){
        $this->isLogin();
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new GoodsModel();
            $res = $res->getActive($id);
        }
        return $res;
    }

    // 取消必抢
    public function cancelActice(){
        $this->isLogin();
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new GoodsModel();
            $res = $res->getcancelActice($id);
        }
        return $res;
    }

    // 主题推荐
    public function setGoods(){
        $this->isLogin();
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new GoodsModel();
            $res = $res->getPush($id);
        }
        return $res;
    }

    // 取消主题
    public function cancelRecom(){
        $this->isLogin();
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new GoodsModel();
            $res = $res->getcancelRecom($id);
        }
        return $res;
    }

    // 进货管理
    public function initialStock(){
        $this->isLogin();
        $id = input('id');
        $obj = new GoodsModel();
        $res = $obj->getUpdate($id);
        $this->assign('res',$res);
        return $this->fetch('initial_stock');
    }

    //库存增加减少
    public function setStock(){
        $this->isLogin();
        $res = input('res');
        $id = input('id');
        $data = [
            'id' => $id,
            'goods_stock'=>input('num'),
            'initial_stock'=>input('num'),
        ];
        if($res == 'plus'){
            $res = new GoodsModel();
            $res = $res->plusStock($data);
            return $res;
        }else if($res == 'reduce'){
            $res = new GoodsModel();
            $res = $res->reduceStock($data);
            return $res;
        }
    }
}